﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="pragma" content="no-cache">  <!-- HTTP 1.1 -->
    <meta http-equiv="cache-control" content="no-cache,must-revalidate">  <!-- HTTP 1.0 -->
    <meta name="renderer" content="webkit|ie-comp|ie-stand"/> <!--优先级：极速模式，兼容模式，IE模式-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <title>扬州房产测绘业务管理系统</title>
    <link rel="stylesheet" type="text/css" href="../../libs/jeasyui-181/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../libs/jeasyui-181/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../../libs/jeasyui-181/themes/color.css">
    <link rel="stylesheet" type="text/css" href="../../css/common.css">
    <script type="text/javascript" src="../../libs/jeasyui-181/jquery.min.js"></script>
    <script type="text/javascript" src="../../libs/jeasyui-181/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../libs/thUtil.js"></script>
    <script type="text/javascript">

        var mainGrid = new UserContol();
        var nodeArr = [];

        $(function () {
            if (UrlParm.hasParm('text')) {
                $('h1').html(UrlParm.parm('text'));
            }

            $('#insert_Link').html(button.insert).linkbutton({onClick: ajax.insert});
            $('#update_Link').html(button.insert).linkbutton({onClick: ajax.workflowIsRunning});
            $('#delete_Link').html(button.insert).linkbutton({onClick: ajax.delete});
            $('#resort_Link').html(button.resort).linkbutton({onClick: ajax.resort});

            mygrid.init();
        });

        var ajax = {
                getList: function (param, success) {
                    Network.connect(window.api.modelSelectPageList, 'POST', param, function (res) {
                        if (res.code === ResponseCode.OK) {
                            success(res.data);
                        }
                    });
                },
                insert: function () {
                    dlg = top.$.hDialog({
                        href: window.router.modelForm + '?n=' + Math.random(),
                        title: '新增模型',
                        width: 700,
                        height: 350,
                        onLoad: function () {
                            var row = mygrid.selectRow();
                            ajax.getProcessNameList(function (res) {
                                if (res.code === ResponseCode.OK) {
                                    var processNameList = res.data.rows;
                                    for (var i = 0; i < processNameList.length; i++) {
                                        var parentCode = processNameList[i].parentCode;
                                        if (parentCode !== null && parentCode !== '') {
                                            top.$('#processNameRadio').append('<input type="radio" name="processName" ' +
                                                'value="' + processNameList[i].code + '"><span>' + processNameList[i].value + '</span><br/>');
                                        }
                                    }
                                    initView.initCombo(processNameList);
                                }

                            });

                        },
                        submit: function () {
                            var isValid = top.$('#form').form("validate");
                            if (isValid) {

                                var formData = top.$("#form").serializeJson();
                                //console.log(formData);
                                Network.connect(window.api.modelInsert, 'POST', formData, function (res) {
                                    if (res.code === ResponseCode.OK) {
                                        //  Tools.show('添加成功');
                                        dlg.dialog('close');
                                        window.location.href = "../modeler.html?modelId=" + res.data;
                                        // mygrid.reload();
                                    }
                                });
                            }
                        }
                    });
                },
                update: function (id) {
                    location.href = '../modeler.html?modelId=' + id;
                },
                delete: function () {
                    var rows = $('#mainGrid').datagrid("getSelections");
                    if (rows.length === 1) {
                        Tools.confirm('是否确定删除该数据？', function (result) {
                            if (result) {
                                var formData = {modelId: rows[0].id};
                                Network.connect(window.api.modelDelete, 'POST', formData, function (res) {
                                    if (res.code === ResponseCode.OK) {
                                        Tools.show('删除成功');
                                        mygrid.reload();
                                    }
                                });
                            }
                        });
                    }
                    else {
                        Tools.alert('请选择需要删除的数据。');
                    }
                },
                resort: function () {
                    Tools.confirm('是否确定重新整理数据？', function (result) {
                        if (result) {
                            Network.connect(window.api.groupReSort, 'POST', {}, function (res) {
                                if (res.code === ResponseCode.OK) {
                                    Tools.show('重新整理成功');
                                    mygrid.reload();
                                }
                            });
                        }
                    });
                },
                search: function (id) {
                    dlg = top.$.hDialog({
                            href: window.router.modelView + '?n=' + Math.random(),
                            title: '流程模型信息',
                            width: 1050,
                            height: 600,
                            onLoad: function () {
                                var row = mygrid.selectRow();
                                var param = {id:row.id,deploymentId:row.deploymentId};
                                //初始值
                                top.$('#deploymentId').val(row.deploymentId);
                                //加载流程图
                                Network.connect(window.api.processPic, "POST", param, function (res) {
                                    if (res.code === ResponseCode.OK) {
                                        top.$('#processPic').prop('src', 'data:image/png;base64,' + res.data);
                                    }
                                });
                                //加载流程节点名称
                                Network.connect(window.api.getNodeName, 'POST', param, function (res) {
                                    if (res.code === ResponseCode.OK) {
                                        nodeArr = res.data.rows;
                                        var html = "";
                                        for (var i = 0; i < nodeArr.length; i++) {
                                            html +=
                                                '<tr>' +
                                                '<td>' +
                                                '<div>' +
                                                '<input type="hidden" id="' + nodeArr[i].overrideId + 'Input" name="' + nodeArr[i].overrideId + 'Input" value="">' +
                                                '<div class="input-box" style="width: 400px;display: inline-block;">' +
                                                '<select class="easyui-combobox" name="' + nodeArr[i].overrideId + '" id="' + nodeArr[i].overrideId + '"' +
                                                ' data-options="' +
                                                'width: 400,' +
                                                'labelWidth: 150,' +
                                                'labelPosition: \'left\',' +
                                                'required: true,' +
                                                'label: \'' + nodeArr[i].name + ':\',' +
                                                'panelHeight: \'auto\',' +
                                                'valueField: \'id\',' +
                                                'textField: \'text\',' +
                                                'editable: false,"></select>' +
                                                '</div>' +
                                                '<div style="display: inline-block;margin-left: 10px">' +
                                                '<select class="easyui-combotree" name="' + nodeArr[i].overrideId + 'tree" id="' + nodeArr[i].overrideId + 'tree"' +
                                                ' data-options=\"  width: 250,' +
                                                'required: true,' +
                                                'panelHeight: \'300\',' +
                                                'valueField: \'id\',' +
                                                'textField: \'text\',' +
                                                'editable: false,\"></select>' +
                                                '</div>' +
                                                '</div>' +
                                                '</td>' +
                                                '</tr>';
                                        }
                                        top.$("#nodeTable").append(html);
                                        //重新加载ui元素，使用循环遍历避免页面源样式元素验证框遗留在界面
                                        var arr = [{id: "EXECUTOR_TYPE_DEPT", text: "部门"}, {
                                            id: "EXECUTOR_TYPE_ROLE",
                                            text: "角色"
                                        },
                                            {id: "EXECUTOR_TYPE_SINGLE", text: "个人"}, {id: "EXECUTOR_TYPE_FUNC", text: "接口"}];

                                        for (var j = 0; j < nodeArr.length; j++) {
                                            var nodeInfo = nodeArr[j];
                                            (function (nodeInfo) {
                                                //初始化一级选择器
                                                top.$("#" + nodeInfo.overrideId).combobox().combobox('loadData', arr).combobox({
                                                    onChange: function (newVal, oldVal) {
                                                        //部门结构
                                                        if (newVal === "EXECUTOR_TYPE_DEPT") {
                                                            globalAjax.groupGetGroupTreeList(function (res) {
                                                                top.$("#" + nodeInfo.overrideId + "tree").combotree('loadData', res.data);
                                                            });
                                                        } else {
                                                            //top.$("#" + nodeInfo.overrideId + "tree")
                                                        }

                                                    }
                                                }).combobox("setValue", nodeInfo.executorType);
                                            })(nodeInfo);
                                            //初始化二级选择器
                                            top.$("#" + nodeArr[j].overrideId + "Input").val(nodeArr[j].id);
                                            top.$("#" + nodeArr[j].overrideId + "tree").combotree().combotree("setValue", nodeArr[j].executorVal);

                                        }
                                    }
                                });
                            },
                            submit: function () {
                                var isValid = top.$('#form').form("validate");
                                if (isValid) {
                                    var executor = [];
                                    var formData = top.$("#form").serializeJson();
                                    var deploymentId = top.$("#deploymentId").val();
                                    //id type val nodeId
                                    for (var i = 0; i < nodeArr.length; i++) {
                                        var type = formData[nodeArr[i].overrideId];
                                        var val = formData[nodeArr[i].overrideId + 'tree'];
                                        executor.push({
                                            id: nodeArr[i].id,
                                            deploymentId: deploymentId,
                                            type: type,
                                            val: val,
                                            nodeId: nodeArr[i].overrideId
                                        });
                                    }
                                    Network.connect(window.api.saveExecutor, 'POST', executor, function (res) {
                                        if (res.code === ResponseCode.OK) {
                                            //console.log(formData);
                                            Tools.show('添加成功');
                                            dlg.dialog('close');
                                        }
                                    });
                                }
                            }
                        }
                    )
                    ;
                },
                deploy: function (id, flag) {
                    if (flag === "remove") {
                        Tools.confirm('警告！这将删除已运行的流程案件', function (result) {
                            if (result) {
                                var formData = {modelId: id};
                                Network.connect(window.api.modelDeploy, 'POST', formData, function (res) {
                                    if (res.code === ResponseCode.OK) {
                                        mygrid.reload();
                                    }
                                    Tools.show(res.data);
                                });
                            }
                        });
                    } else {
                        var formData = {modelId: id};
                        Network.connect(window.api.modelDeploy, 'POST', formData, function (res) {
                            if (res.code === ResponseCode.OK) {
                                mygrid.reload();
                            }
                            Tools.show(res.data);
                        });
                    }
                }
                ,
                workflowIsRunning: function () {
                    var rows = $('#mainGrid').datagrid("getSelections");
                    if (rows.length === 1) {
                        var formData = {modelId: rows[0].id};
                        Network.connect(window.api.workflowIsRunning, 'POST', formData, function (res) {
                            if (res.code === ResponseCode.OK) {
                                ajax.update(rows[0].id);
                            } else {
                                Tools.show(res.message);
                            }
                        });
                    }
                    else {
                        Tools.alert('请选择需要编辑的数据。');
                    }

                }
                ,
                getProcessNameList: function (methods) {
                    Network.connect(window.api.enumNoPageList, "POST", {parentCode: "PROCESS_NAME"}, methods)
                }

            }
        ;

        var mygrid = {
            init: function () {
                mainGrid.dataGrid.id = 'mainGrid';
                mainGrid.dataGrid.idField = 'ID';
                //   mainGrid.dataGrid.sortName = 'USERID';
                mainGrid.dataGrid.sortOrder = 'asc';
                mainGrid.dataGrid.toolbar = '#toolbar';
                mainGrid.dataGrid.onLoadSuccess = function (data) {
                    if (data.total === 0)
                    //setEmptyData($(this));
                    //console.log(JSON.stringify(data));
                        for (var i = 0; i < data.rows.length; i++) {//for循环闭包
                            var id = data.rows[i].id;
                            (function (id) {
                                $("#start" + id).switchbutton({
                                    onChange: function (checked) {
                                        ajax.operate(id, "start");
                                    }
                                });
                            })(id);
                        }
                    $(".easyui-linkbutton").linkbutton();

                };
                mainGrid.dataGrid.loader = function (param, success, error) {
                    ajax.getList(param, success);
                };
                mainGrid.dataGrid.columns = [[
                    {field: 'id', title: '编号', halign: 'center', align: 'center', width: 100, iconCls: 'icon-cancel'},
                    {field: 'name', title: '名称', halign: 'center', align: 'left', width: 250},
                    {field: 'key', title: '模键', halign: 'center', align: 'left', width: 150},
                    {field: 'deploymentId', title: '发布id', halign: 'center', align: 'center', width: 100},
                    {field: 'createTime', title: '创建时间', halign: 'center', align: 'center', width: 300},
                    {
                        field: 'description', title: '描述', halign: 'center', align: 'left', width: 400,
                        formatter: function (value, row) {
                            return row.metaInfo.description;
                        }
                    },
                    {
                        field: 'status', title: '状态', halign: 'center', align: 'center', width: 100,
                        formatter: function (value, row, index) {
                            var status = row.status;
                            if (status === "草稿") {
                                status = '<div class="draft"><legend>草稿</legend></div>';
                            } else if (status === "已发布") {
                                status = '<div class="deploy"><legend>已发布</legend></div>';
                            } else if (status === "运行中") {
                                status = '<div class="running"><legend>运行中</legend></div>';
                            } else {
                                status = '<div class="exception"><legend>异常</legend></div>';
                            }
                            return status;
                        }
                    },
                    {
                        field: 'operation', title: '操作', halign: 'center', align: 'center', width: 250,
                        formatter: function (value, row, index) {
                            var deployHtml;
                            var deploymentId = row.deploymentId;
                            if (deploymentId === null || deploymentId === '') {
                                deployHtml = '<a class="easyui-linkbutton" data-options="iconCls:\'icon-add\'" href="javascript:void(0)" onclick="ajax.deploy(\'' + row.id + '\',\'deploy\')" >发布</a>';
                            } else {
                                deployHtml = '<a class="easyui-linkbutton" data-options="iconCls:\'icon-remove\'" href="javascript:void(0)" onclick="ajax.deploy(\'' + row.id + '\',\'remove\')" >移除</a>';
                            }
                            var searchHtml = '<a class="easyui-linkbutton" data-options="iconCls:\'icon-search\'" href="javascript:void(0)" onclick="ajax.search(\'' + row.id + '\',1)" >查看</a>';
                            return searchHtml + deployHtml;

                        }
                    }
                ]];
                mainGrid.InitdataGrid();
            },
            reload: function () {
                $('#mainGrid').datagrid('reload');
            },
            selectRow: function () {
                return $('#mainGrid').datagrid('getSelected');
            }
        };

        var initView = {
            initCombo: function (processNameList) {
                top.$('#name').combo({
                    required: true,    //不能为空
                    multiple: false     //是否支持多选
                }).combo({
                    onChange: function (newVal, oldVal) {
                        var code;
                        var description;
                        for (var i = 0; i < processNameList.length; i++) {
                            var processName = processNameList[i];
                            if (processName.value === newVal) {
                                code = processName.code;
                                description = processName.rem;
                            }
                        }
                        var modelKey = code + '_' + new Date().getTime();
                        top.$('#modelKey').textbox('setValue', modelKey);
                        top.$('#description').textbox('setValue', description);
                    }
                });
                top.$('#processName').appendTo(top.$('#name').combo('panel'));  //将下拉区块添加到下拉框里
                top.$('#processName input').click(function () {
                    var v = $(this).val();    //获取到选择的value值
                    var s = $(this).next('span').text(); //获取span里的内容
                    //将value值添加到组件value里，将span里的内容添加是组件输入框，最后关闭下拉面板
                    top.$('#name').combo('setValue', s).combo('setText', s).combo('hidePanel');
                })
            }
        }

    </script>

</head>
<body>
<div class="container-wrap">
    <div id="toolbar">
        <div class="container-tit">
            <h1></h1>
        </div>
        <div class="sreach fixed">
            <!--<input type="text" class="easyui-textbox" data-options="label:'上级部门:',labelWidth: 80,width:240,"/>
            <a href="#" class="easyui-linkbutton search">查询</a>-->
            <div class="bar-btn">
                <a id="insert_Link" href="#" class="easyui-linkbutton insert">新增</a>
                <a id="update_Link" href="#" class="easyui-linkbutton update">编辑</a>
                <a id="delete_Link" href="#" class="easyui-linkbutton delete">删除</a>
                <a id="resort_Link" href="#" class="easyui-linkbutton update">重载</a>
            </div>
        </div>
    </div>
    <div id="mainGrid"></div>
</div>
</body>
</html>